﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Lib.Utils;
using Lib.Bases;

namespace MS.INV.MainData
{
    public partial class Catalog_New : Lib.Bases.Forms.FBaseEdit
    {
        SysObj.L_HangHoa cls = new SysObj.L_HangHoa();

        public Catalog_New(int _ObjID)
        {
            InitializeComponent();

            cbDVT.BaseList = new SysLst.L_DVT();
            cbLoai.BaseList = new SysLst.L_LoaiHang();
            cbNhom.BaseList = new SysLst.L_NhomHang();

            this._BaseClass = cls;

            this.ObjID = _ObjID;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            try
            {
                if (this.ObjID > 0)
                {
                    cls.IdHangHoa = ObjID;
                    cls.ReadData();

                    txtMa.Text = cls.MaHang;
                    txtTen.Text = cls.TenHang;
                    txtGhiChu.Text = cls.GhiChu;

                    cbDVT.EditValue = cls.IdDVT;
                    cbLoai.EditValue = cls.IdLoai;
                    cbNhom.EditValue = cls.IdNhom;

                    chkTD.Checked = cls.ThucDon;
                    chkNoStock.Checked = cls.NoStock;

                    if(!Is.IsNullOrEmpty(cls.HinhAnh))
                    {
                        using (MemoryStream stream = new MemoryStream(cls.HinhAnh))
                        {
                            pic.Image = System.Drawing.Image.FromStream(stream);
                        }
                    }

                    btnUpdate.Enabled = true;
                }
                else
                    T_NewItemClick(null, null);
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "OnLoad");
            }
        }

        public override void T_NewItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.T_NewItemClick(sender, e);
        }

        public override void T_UpdateItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (!Ctrls.CheckAllowNull(this))
                    return;
                cls.MaHang = txtMa.Text;
                cls.TenHang = txtTen.Text;
                cls.IdDVT = Cvert.ToInt32(cbDVT.EditValue, 0);
                cls.IdLoai = Cvert.ToInt32(cbLoai.EditValue, 0);
                cls.IdNhom = Cvert.ToInt32(cbNhom.EditValue, 0);
                cls.ThucDon = chkTD.Checked;
                cls.NoStock = chkNoStock.Checked;
                cls.GhiChu = txtGhiChu.Text;
                if (pic.Image != null)
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        //ImageConverter converter = new ImageConverter();

                        pic.Image.Save(ms, pic.Image.RawFormat);
                        //cls.HinhAnh = (byte[])converter.ConvertTo(pic.Image, typeof(byte[]));//
                        cls.HinhAnh = ms.ToArray();
                    }
                }
                else
                    cls.HinhAnh = new byte[0];
                base.T_UpdateItemClick(sender, e);
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "Update data");
            }
        }

        private void btPath_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Select a image.";
            if (Directory.Exists(Application.StartupPath + @"\Images"))
                ofd.InitialDirectory = Application.StartupPath + @"\Images";

            ofd.Filter = "Image Files|*.bmp;*.jpg;*.jpeg;*.png;*.gif;*.tif;*.tiff";
            ofd.ShowDialog();
            if (!Is.IsNullOrEmpty(ofd.FileName))
            {
                //MemoryStream ms = new MemoryStream();
                //Image imageIn = Image.FromFile(ofd.FileName);
                //imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                //row["Image"] = ms.ToArray();
                pic.Image = Image.FromFile(ofd.FileName);
            }
        }

        private void btClear_Click(object sender, EventArgs e)
        {
            pic.Image = null;
        }
    }
}
